package com.hongguan.oms.web.contect;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.common.Const;
import com.hongguan.oms.domain.UserInfo;
import com.hongguan.oms.service.contect.ContectService;
import com.hongguan.oms.utils.Util;
import com.hongguan.oms.web.BaseHandler;

/**
 * 
 * @author tonylin <br>
 * @create 2011-1-7 <br>
 * @project Haojin <br>
 * @copyright 2011 HongGuan Information. All rights reserved
 * @desc <br>
 */
public class ContectHead extends BaseHandler {

	private ContectService contectService;

	public void setContectService(ContectService contectService) {
		this.contectService = contectService;
	}

	public ModelAndView updateContect(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv =  new ModelAndView("/updateContect");
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();
			String ContectID = request.getParameter("ids");
			String[] ContectIDArray = ContectID.split(",");
			
			UserInfo user = (UserInfo) request.getSession().getAttribute(
					Const.SESSION_USER);
			String name=user.getUserName();
			
			
			for (int i = 0; i < ContectIDArray.length; i++) {
				map.put("ContectID", ContectIDArray[i]);
				if (name.equals("黄怡国")){
				
				if (request.getParameter("BossAdvice") != null) {
					map.put("BossAdvice", request
							.getParameter("BossAdvice"));
				}
				}else{
				if (request.getParameter("DepartmentAdvice") != null) {
					map.put("DepartmentAdvice", request
							.getParameter("DepartmentAdvice"));
				}
				}
				map.put("LastUpdateBy", Util.getUserIdFromSession(request));
				map.put("LastUpdateTime", new SimpleDateFormat("yyyy-MM-dd")
						.format(date));
				contectService.updateContect(map);
			}
			mv.addObject(SUCCESS, "true");
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}

		return mv;

	}
	public ModelAndView getUserInfo(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		mv.addObject("result",user);
		
		return mv;
	}
	
	

	public ModelAndView addContect(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();

			if (request.getParameter("projectContectDate").equals("")) {
			} else {
				map.put("projectContectDate", request
						.getParameter("projectContectDate"));
			}
			if (request.getParameter("projectTopic") != null) {
				map.put("projectTopic", request.getParameter("projectTopic"));
			}
			if (request.getParameter("projectNewlast") != null) {
				map.put("projectNewlast", request
						.getParameter("projectNewlast"));
			}
			if (request.getParameter("projectDangerous") != null) {
				map.put("projectDangerous", request
						.getParameter("projectDangerous"));
			}
			if (request.getParameter("customerID") != null) {
				map.put("customerID", request.getParameter("customerID"));
			}

			map.put("CreateBy", Util.getUserIdFromSession(request));
			map.put("CreateTime", new SimpleDateFormat("yyyy-MM-dd")
					.format(date));
			map.put("ContectPerson", user.getUserName());
			map.put("departname", user.getBranchName());
			

			contectService.addContect(map);
			mv.addObject(SUCCESS, "true");
			
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(ERROR, "false");

		}
		return mv;
	}

	public ModelAndView searchContect(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		Map<String, Object> map = new HashMap<String, Object>();
		
		
		try {
			if ("".equals(request.getParameter("customerID"))) {

			} else {
				map.put("customerID", request.getParameter("customerID"));
			}

			if ("".equals(request.getParameter("customerName"))) {
			} else {
				map.put("customerName", request.getParameter("customerName"));
			}
			if (request.getParameter("startContectDate").equals("")) {
			} else {
				map.put("startContectDate", request
						.getParameter("startContectDate"));
			}
			if (request.getParameter("endContectDate").equals("")) {
			} else {
				map.put("endContectDate", request
						.getParameter("endContectDate"));
			}
			map.put("SaleStage", request.getParameter("SaleStage"));
		
			UserInfo user = (UserInfo) request.getSession().getAttribute(
					Const.SESSION_USER);
			String name=user.getUserName();
			String depart=user.getBranchName();
			if (name.equals("黄怡国")){
				mv.addObject("result", contectService.searchContect(map));
			}else{
				map.put("depart", depart);
				mv.addObject("result", contectService.searchContect(map));
			}
			mv.addObject(SUCCESS, "true");
		} catch (Exception e) {
			e.printStackTrace();

		}
		return mv;

	}
}
